<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.bluesky.light.category.*"%>
<%@ page import="com.bluesky.light.product.*" %>
<%@ page language="java" import="com.bluesky.light.admin.*"%>
<%
	request.setCharacterEncoding("UTF-8");
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
	Admin admin = (Admin)session.getAttribute("admin");
	if (admin == null) {
		response.sendRedirect("index.jsp");
		return;
	}
%>
<%
int categoryId = -1;
String strCategoryId = request.getParameter("categoryId");
if (strCategoryId != null && !strCategoryId.trim().equals("")) {
	categoryId = Integer.parseInt(strCategoryId);
}

String action = request.getParameter("action");

if (action != null && action.trim().equals("productadd")) {
	String name = request.getParameter("name");
	double cost = Double.parseDouble(request.getParameter("cost"));
	double price = Double.parseDouble(request.getParameter("price"));
	String descr = request.getParameter("descr");
	int inven = Integer.parseInt(request.getParameter("inven"));
	
	Product p = new Product();
	p.setName(name);
	p.setDescr(descr);
	p.setCost(cost);
	p.setPrice(price);
	p.setCategoryId(categoryId);
	p.setInven(inven);
	p.setPdate(new Date());
	ProductService.getInstance().add(p);
	response.sendRedirect("product_add.jsp");
}
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>light - register</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link href="admin/css/main.css" rel="stylesheet" type="text/css" />
	
	<script language=JavaScript  src="admin/js/checkproductadd.js"> 
	</script>
	
  </head>
  
  <body>
	<!-- header -->
	<div id="header">
		<div id="logo"><img src="images/logo_small2.png"/></div>
	</div>
	<!-- // header -->
	<!-- nav -->
	<div id="nav">
	</div>
	<!-- // nav -->
	<!-- container -->
	<div id="container">
		<!-- contents -->
		<div id="contents">
			<div id="regist_form">
	        	<form method="post" name="categoryadd" action="admin/product_addInput.jsp" onSubmit="return checkdata()">
	        		<input type="hidden" name="action" value="productadd"/>
	            	<h2>Add a Product</h2>
	            	<table>	            		
	                	<tr><td>Name: </td><td><input name="name" type="text"/></td></tr>
	                    <tr><td>Description: </td><td><textarea name="descr" cols="40" rows="5"></textarea></td></tr>
	                    <tr><td>Cost: </td><td><input name="cost" type="text"/></td></tr>
	                    <tr><td>Price: </td><td><input name="price" type="text"/></td></tr>
	                    <tr><td>inventory: </td><td><input name="inven" type="text"/></td></tr>
	                    <tr>
	                    	<td>Category: </td>
	                    	<td>
	                    	<select name="categoryId">
	                    	<%
	                    		List<Category> categories = CategoryService.getInstance().getCategories();
	                    		for (Iterator<Category> it = categories.iterator(); it.hasNext(); ) {
	                    			Category c = it.next();
	                    			String selected="";
	                    			if (c.getId() == categoryId) selected = "selected";
	                    			String preStr = "";
	                    			for (int i=1; i<c.getGrade(); i++) preStr += "--";
	                    	%>
	                    	<option value="<%=c.getId()%>" <%=selected%>><%=preStr + c.getName()%></option>
	                    	<%
	                    		}
	                    	%>
	                    	</select>
	                    	</td>
	                    </tr>
	                    <tr><td/><td/></tr>                  
	                </table>
	                <center><input name="regsubmit" type="submit" value="OK! Add a new product!"/></center>
	            </form>
	        </div>
	    </div>
	    <!-- // contents -->
	</div>
	<!-- // container -->
	<div id="footer">
		<div id="bs_stdio">
	    	©2011-2011 www.bluesky.com.mo All Rights Reserved<br/>
	    	<img src="images/bluesky_logo.png" />
	    </div>
	</div>
  </body>
</html>
